<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.2">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="数据库软件：一个软件,看的见,可操作,实现数据库的逻辑功能数据库：是一种逻辑概念,用来存放数据的仓库,侧重存储数据仓库：从数据量上来说,比数据库庞大的多,主要用于数据分析和">
<meta name="keywords" content="数据库,MySQL">
<meta property="og:type" content="article">
<meta property="og:title" content="Mysql-教程01">
<meta property="og:url" content="https://rencheng.cc/2019/07/02/mysql/教程01/index.html">
<meta property="og:site_name" content="PHOENIX&#39;s Blog">
<meta property="og:description" content="数据库软件：一个软件,看的见,可操作,实现数据库的逻辑功能数据库：是一种逻辑概念,用来存放数据的仓库,侧重存储数据仓库：从数据量上来说,比数据库庞大的多,主要用于数据分析和">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-07-02T12:02:43.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mysql-教程01">
<meta name="twitter:description" content="数据库软件：一个软件,看的见,可操作,实现数据库的逻辑功能数据库：是一种逻辑概念,用来存放数据的仓库,侧重存储数据仓库：从数据量上来说,比数据库庞大的多,主要用于数据分析和">



  <link rel="alternate" href="/atom.xml" title="PHOENIX's Blog" type="application/atom+xml">



  
  
  <link rel="canonical" href="https://rencheng.cc/2019/07/02/mysql/教程01/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Mysql-教程01 | PHOENIX's Blog</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">PHOENIX's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">Enjoy the journey of life</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://rencheng.cc/2019/07/02/mysql/教程01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="任成">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="PHOENIX's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Mysql-教程01

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-07-02 20:02:43" itemprop="dateCreated datePublished" datetime="2019-07-02T20:02:43+08:00">2019-07-02</time>
            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/教程/" itemprop="url" rel="index"><span itemprop="name">教程</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          
            <div class="post-symbolscount">
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                <span title="本文字数">4.9k</span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>数据库软件：一个软件,看的见,可操作,实现数据库的逻辑功能<br>数据库：是一种逻辑概念,用来存放数据的仓库,侧重存储<br>数据仓库：从数据量上来说,比数据库庞大的多,主要用于数据分析和</p>
<a id="more"></a>
<h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><h3 id="什么是数据库"><a href="#什么是数据库" class="headerlink" title="什么是数据库"></a>什么是数据库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">存储数据的仓库</span><br></pre></td></tr></table></figure>
<h3 id="都有哪些公司在用数据库"><a href="#都有哪些公司在用数据库" class="headerlink" title="都有哪些公司在用数据库"></a>都有哪些公司在用数据库</h3><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">金融机构、游戏网站、购物网站、论坛网站 <span class="string">...</span> <span class="string">...</span></span><br></pre></td></tr></table></figure>
<h3 id="提供数据库服务的软件"><a href="#提供数据库服务的软件" class="headerlink" title="提供数据库服务的软件"></a>提供数据库服务的软件</h3><ol>
<li>软件分类</li>
</ol>
<figure class="highlight tp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MySQL、Oracle、SQL_Server、<span class="keyword">DB</span><span class="number">2</span>、MongoDB、MariaDB</span><br></pre></td></tr></table></figure>
<ol>
<li>生产环境中,如何选择使用哪个数据库软件<ol>
<li>是否开源<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">  1. </span>开源软件：MySQL、Mariadb、MongoDB</span><br><span class="line"><span class="bullet">2. </span>商业软件：Oracle、DB2、SQL_Server</span><br></pre></td></tr></table></figure>
</li>
</ol>
</li>
</ol>
<pre><code>  1. 是否跨平台
    <figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">1. </span>不跨平台 ：SQL_Server</span><br><span class="line"><span class="bullet">2. </span>跨平台 ：...</span><br></pre></td></tr></table></figure>


  1. 公司的类型
    <figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">1. </span>商业软件 ：政府部门、金融机构</span><br><span class="line"><span class="bullet">2. </span>开源软件 ：游戏网站、购物网站、论坛网站 ..</span><br></pre></td></tr></table></figure>
</code></pre><h3 id="MySQL的特点"><a href="#MySQL的特点" class="headerlink" title="MySQL的特点"></a>MySQL的特点</h3><h4 id="关系型数据库"><a href="#关系型数据库" class="headerlink" title="关系型数据库"></a>关系型数据库</h4><pre><code><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">1. </span>数据是以行和列(表格)的形式去存储的</span><br><span class="line"><span class="bullet">2. </span>表中的每一行叫一条记录,每一列叫一个字段</span><br><span class="line"><span class="bullet">3. </span>表和表之间的逻辑关联叫关系</span><br></pre></td></tr></table></figure>


**示例**
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">关系型数据库存储</span><br><span class="line"></span><br><span class="line">表<span class="number">1</span>、学生信息表</span><br><span class="line"> 姓名   年龄  班级</span><br><span class="line"> 星矢    <span class="number">25</span>   三班</span><br><span class="line"> 水冰月  <span class="number">25</span>   六班</span><br><span class="line"></span><br><span class="line">表<span class="number">2</span>、班级信息表</span><br><span class="line"> 班级   班主任</span><br><span class="line"> 三班   大空翼</span><br><span class="line"> 六班   松人</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"> 非关系型数据库中存储</span><br><span class="line"> </span><br><span class="line"> &#123;<span class="string">"姓名"</span>:<span class="string">"水冰月"</span>,<span class="string">"年龄"</span>:25,<span class="string">"班级"</span>:<span class="string">"六班"</span>&#125;</span><br><span class="line">&#123;<span class="string">"姓名"</span>:<span class="string">"星矢"</span>,<span class="string">"年龄"</span>:25,<span class="string">"班级"</span>:<span class="string">"三班"</span>,<span class="string">"班主任"</span>:<span class="string">"大空翼"</span>&#125;</span><br></pre></td></tr></table></figure>
</code></pre><h5 id="跨平台"><a href="#跨平台" class="headerlink" title="跨平台"></a>跨平台</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">可以在Unix、Linux、Windows上运行数据库服务</span><br></pre></td></tr></table></figure>
<h5 id="支持多种编程语言"><a href="#支持多种编程语言" class="headerlink" title="支持多种编程语言"></a>支持多种编程语言</h5><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python、java、php <span class="string">...</span> <span class="string">...</span></span><br></pre></td></tr></table></figure>
<h4 id="数据库软件、数据库、数据仓库的概念"><a href="#数据库软件、数据库、数据仓库的概念" class="headerlink" title="数据库软件、数据库、数据仓库的概念"></a>数据库软件、数据库、数据仓库的概念</h4><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">1. </span>数据库软件</span><br><span class="line"><span class="code">    一个软件,看的见,可操作,实现数据库的逻辑功能</span></span><br><span class="line"><span class="bullet">2. </span>数据库</span><br><span class="line"><span class="code">      是一种逻辑概念,用来存放数据的仓库,侧重存储</span></span><br><span class="line"><span class="bullet">3. </span>数据仓库</span><br><span class="line"><span class="code">    从数据量上来说,比数据库庞大的多,主要用于数据分析和数据挖掘</span></span><br></pre></td></tr></table></figure>
<h2 id="MySQL安装"><a href="#MySQL安装" class="headerlink" title="MySQL安装"></a>MySQL安装</h2><h3 id="Ubuntu安装软件"><a href="#Ubuntu安装软件" class="headerlink" title="Ubuntu安装软件"></a>Ubuntu安装软件</h3><ol>
<li><p>更新apt-get</p>
 <figure class="highlight q"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-<span class="built_in">get</span> <span class="keyword">update</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>把本地已安装软件与刚下载的软件列表进行对比,如果发现已安装软件版本低,则更新</p>
 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-<span class="builtin-name">get</span> upgrade</span><br></pre></td></tr></table></figure>
</li>
<li><p>修复依赖关系</p>
 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-<span class="builtin-name">get</span> -f install</span><br></pre></td></tr></table></figure>
</li>
</ol>
<blockquote>
<p>访问源列表中的每个网址,读取软件列表,保存到本地<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### Windows安装</span><br><span class="line">1. 下载MySQL安装包(windows)</span><br><span class="line">      mysql-installer***5.7.***.msi</span><br><span class="line">2. 安装教程去安装</span><br><span class="line"></span><br><span class="line">### Mac安装</span><br><span class="line">1. cd</span><br><span class="line">2. vi .bash_profile</span><br><span class="line">添加 ：PATH=&quot;$PATH&quot;:/usr/local/mysql/bin</span><br><span class="line">3. source .bash_profile</span><br><span class="line"></span><br><span class="line">## 启动和连接MySQL服务</span><br><span class="line">1. 服务端启动</span><br></pre></td></tr></table></figure></p>
</blockquote>
<pre><code>查看MySQL状态
sudo /etc/init.d/mysql status
启动，停止，重启
sudo /etc/init.d/mysql start | stop | restart
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">2. </span>客户端连接</span><br><span class="line"><span class="bullet">    1. </span>命令格式</span><br></pre></td></tr></table></figure>

mysql -h主机地址 -u用户名 -p密码
mysql -hlocalhost -uroot -p123456
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2</span>、本地连接可省略 -h 选项</span><br></pre></td></tr></table></figure>

mysql -uroot -p123456
<figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">    </span><br><span class="line">## 基本SQL命令</span><br><span class="line"></span><br><span class="line">### SQL命令的使用规则</span><br></pre></td></tr></table></figure>
</code></pre><p>1、每条命令必须以 ; 结尾<br>2、SQL命令不区分字母大小写<br>3、使用 \c 来终止当前命令的执行<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### 库的管理</span><br><span class="line">#### 库的基本操作</span><br></pre></td></tr></table></figure></p>
<p>1、查看已有库<br>show databases;<br>2、创建库(指定字符集)<br>create database 库名 [character set utf8];<br>3、查看创建库的语句(字符集)<br>show create database 库名;<br>4、查看当前所在库<br>select database();<br>5、切换库<br>use 库名;<br>6、查看库中已有表<br>show tables;<br>7、删除库<br>drop database 库名;<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 库名的命名规则</span><br></pre></td></tr></table></figure></p>
<p>1、数字、字母、下划线,但不能使用纯数字<br>2、库名区分字母大小写<br>3、不能使用特殊字符和mysql关键字<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### 表的管理</span><br><span class="line"></span><br><span class="line">#### 表的基本操作</span><br></pre></td></tr></table></figure></p>
<p>1、创建表(指定字符集)<br>create table 表名(<br>       字段名 数据类型,<br>       字段名 数据类型,<br>       …<br>       字段名 数据类型<br>);<br>2、查看已有表的字符集<br>show create table 表名;<br>3、查看表结构<br>desc 表名;<br>4、删除表<br>drop table 表名;<br><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&gt;<span class="number">1</span>、所有的数据都是以文件的形式存放在数据库目录下</span><br><span class="line">    <span class="number">2</span>、数据库目录 ：/var/<span class="class"><span class="keyword">lib</span>/<span class="title">mysql</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">#### 表记录管理</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>. 插入(insert)</span><br></pre></td></tr></table></figure></p>
<p>1、insert into 表名 values(值1),(值2),…;<br>2、insert into 表名(字段1,…) values(值1),…;<br><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2</span>. 查询(<span class="keyword">select</span>)</span><br></pre></td></tr></table></figure></p>
<p>1、select * from 表名 [where 条件];<br>2、select 字段1,字段名2 from 表名 [where 条件];<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## 如何更改默认字符集</span><br><span class="line"></span><br><span class="line">方法(通过更改MySQL配置文件实现)</span><br><span class="line">步骤</span><br></pre></td></tr></table></figure></p>
<p>1、获取root权限<br>sudo -i<br>2、cd /etc/mysql/mysql.conf.d/<br>3、备份 ：cp mysqld.cnf mysqld.cnf.bak<br>4、vim mysqld.cnf<br>[mysqld]<br>character_set_server = utf8<br>5、重启mysql服务<br>/etc/init.d/mysql restart<br><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">mac</span>上</span><br></pre></td></tr></table></figure></p>
<p>配置文件路径<br>/etc/my.cnf<br>[mysqld]<br>character_set_server = utf8<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## 客户端把数据存储到数据库服务器上的过程</span><br><span class="line"><span class="number">1.</span> 连接到数据服务器 mysql -uroot -p123456</span><br><span class="line"><span class="number">2.</span> 选择一个库  use 库名</span><br><span class="line"><span class="number">3.</span> 创建表/修改表  update 表名...</span><br><span class="line"><span class="number">4.</span> 断开与数据库连接  exit; | quit; | \q;</span><br><span class="line"></span><br><span class="line">## 数据类型</span><br><span class="line"></span><br><span class="line">### 数值类型</span><br><span class="line">#### 整型</span><br><span class="line"><span class="number">1.</span> int 大整型(<span class="number">4</span>个字节)</span><br><span class="line">取值范围 ：<span class="number">2</span>**<span class="number">32</span> - <span class="number">1</span>(<span class="number">42</span>亿多)</span><br><span class="line"><span class="number">2.</span> tinyint 微小整型(<span class="number">1</span>个字节)</span><br><span class="line">    <span class="number">1.</span> 有符号(signed默认) : <span class="number">-128</span> ~ <span class="number">127</span></span><br><span class="line">    <span class="number">2.</span> 无符号(unsigned) : <span class="number">0</span> ~ <span class="number">255</span></span><br><span class="line">          age tinyint unsigned,</span><br><span class="line"><span class="number">3.</span> smallint 小整型(<span class="number">2</span>个字节)</span><br><span class="line"><span class="number">4.</span> bigint 极大整型(<span class="number">8</span>个字节)</span><br><span class="line"></span><br><span class="line">#### 浮点型</span><br><span class="line"><span class="number">1.</span> float(<span class="number">4</span>个字节,最多显示<span class="number">7</span>个有效位)</span><br><span class="line">字段名 float(m,n)  m-&gt;总位数 n-&gt;小数位位数</span><br><span class="line">	  float(<span class="number">5</span>,<span class="number">2</span>)取值范围？ <span class="number">-999.99</span> ~ <span class="number">999.99</span></span><br><span class="line"><span class="number">2.</span> decimal(最多显示<span class="number">28</span>个有效位)</span><br><span class="line">    <span class="number">1</span>、decimal(m,n)</span><br><span class="line">    <span class="number">2</span>、存储空间(整数、小数分开存储)</span><br><span class="line">	  规则：将<span class="number">9</span>的倍数包装成<span class="number">4</span>个字节</span><br><span class="line">	  </span><br><span class="line">```     </span><br><span class="line">余数    字节</span><br><span class="line">  <span class="number">0</span>       <span class="number">0</span></span><br><span class="line"> <span class="number">1</span><span class="number">-2</span>      <span class="number">1</span></span><br><span class="line"> <span class="number">3</span><span class="number">-4</span>      <span class="number">2</span></span><br><span class="line"> <span class="number">5</span><span class="number">-6</span>      <span class="number">3</span></span><br><span class="line"> <span class="number">7</span><span class="number">-9</span>      <span class="number">4</span></span><br><span class="line"></span><br><span class="line">示例：decimal(<span class="number">19</span>,<span class="number">9</span>)</span><br><span class="line">整数部分：<span class="number">10</span>/<span class="number">9</span>=商<span class="number">1</span>余<span class="number">1</span> <span class="number">4</span>字节+<span class="number">1</span>字节=<span class="number">5</span>字节</span><br><span class="line">小数部分：<span class="number">9</span>/<span class="number">9</span>=商<span class="number">1</span>余<span class="number">0</span> <span class="number">4</span>字节+<span class="number">0</span>字节=<span class="number">4</span>字节</span><br><span class="line">占：<span class="number">9</span>字节</span><br></pre></td></tr></table></figure></p>
<h4 id="字符类型"><a href="#字符类型" class="headerlink" title="字符类型"></a>字符类型</h4><ol>
<li>char(定长)<br>取值范围：1~255;浪费存储空间,性能高</li>
<li>varchar(变长)<br>取值范围：1~65535;节省存储空间,性能低</li>
<li>text / longtext(4G) / blob / longblob(4G)</li>
</ol>
<h5 id="区别："><a href="#区别：" class="headerlink" title="区别："></a>区别：</h5><ol>
<li>字符类型宽度和数值类型宽度的区别</li>
<li>数值类型宽度为显示宽度,只用于select查询显示,和占用存储无关,可用zerofill查看效果</li>
<li>字符类型的宽度超过之后则无法存储</li>
</ol>
<h4 id="枚举类型"><a href="#枚举类型" class="headerlink" title="枚举类型"></a>枚举类型</h4><ol>
<li>单选(enum) ：字段名 enum(值1,值2,…)</li>
<li>多选(set)  ：字段名 set(值1,值2,…)</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> t5(</span><br><span class="line"><span class="keyword">id</span> <span class="built_in">int</span>(<span class="number">3</span>) zerofill,</span><br><span class="line"><span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">15</span>),</span><br><span class="line">sex enum(<span class="string">"M"</span>,<span class="string">"F"</span>,<span class="string">"Secret"</span>),</span><br><span class="line">likes <span class="keyword">set</span>(<span class="string">"F"</span>,<span class="string">"M"</span>,<span class="string">"study"</span>,<span class="string">"Python"</span>)</span><br><span class="line">);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> t5(likes) <span class="keyword">values</span>(<span class="string">"F,study,Python"</span>);</span><br></pre></td></tr></table></figure>
<h4 id="日期时间类型"><a href="#日期时间类型" class="headerlink" title="日期时间类型"></a>日期时间类型</h4><ol>
<li>date ：”YYYY-MM-DD”</li>
<li>time ：”HH:MM:SS”</li>
<li>datetime ：”YYYY-MM-DD HH:MM:SS”</li>
<li>timestamp ：”YYYY-MM-DD HH:MM:SS”</li>
<li>注意<ol>
<li>datetime ：不给值默认返回NULL值</li>
<li>timestamp ：不给值默认返回系统当前时间</li>
</ol>
</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> t7(</span><br><span class="line"><span class="keyword">id</span> <span class="built_in">int</span>,</span><br><span class="line"><span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">15</span>),</span><br><span class="line">birthday <span class="built_in">date</span>,</span><br><span class="line">money <span class="built_in">int</span>,</span><br><span class="line">shijian datetime</span><br><span class="line">);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> t7 <span class="keyword">values</span>(<span class="number">2</span>,<span class="string">"王"</span>,<span class="built_in">date</span>(<span class="keyword">now</span>()),<span class="number">10000</span>,<span class="keyword">now</span>());</span><br></pre></td></tr></table></figure>
<h5 id="日期时间函数"><a href="#日期时间函数" class="headerlink" title="日期时间函数"></a>日期时间函数</h5><ol>
<li><strong>now()</strong>  返回服务器当前时间</li>
<li><strong>curdate()</strong> 返回当前日期</li>
<li><strong>curtime()</strong> 返回当前时间</li>
<li><strong>year(date)</strong> 返回指定时间的年份</li>
<li><strong>date(date)</strong> 返回指定时间的日期</li>
<li><strong>time(date)</strong> 返回指定时间的时间</li>
<li>示例<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">1、在表中插入3条记录</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> t7 <span class="keyword">values</span></span><br><span class="line">(<span class="number">3</span>,<span class="string">"小昭"</span>,<span class="number">19000520</span>,<span class="number">3000</span>,<span class="number">20180630000000</span>),</span><br><span class="line">(<span class="number">4</span>,<span class="string">"赵敏"</span>,<span class="number">19000521</span>,<span class="number">4000</span>,<span class="number">20180702000000</span>),</span><br><span class="line">(<span class="number">5</span>,<span class="string">"周芷若"</span>,<span class="number">19010522</span>,<span class="number">3500</span>,<span class="number">20180702100000</span>);</span><br><span class="line">2、查找2018年7月2日有哪些用户充值了</span><br><span class="line">   <span class="keyword">select</span> * <span class="keyword">from</span> t7 <span class="keyword">where</span> <span class="built_in">date</span>(shijian)=<span class="string">"2018-07-02"</span>;</span><br><span class="line"></span><br><span class="line">    3、查找2018年7月份充值的信息</span><br><span class="line">      <span class="keyword">select</span> * <span class="keyword">from</span> t7 </span><br><span class="line">      <span class="keyword">where</span> </span><br><span class="line">      <span class="built_in">date</span>(shijian)&gt;=<span class="string">"2018-07-01"</span> <span class="keyword">and</span> <span class="built_in">date</span>(shijian)&lt;=<span class="string">"2018-07-31"</span>;</span><br><span class="line"></span><br><span class="line">    4、查找7月30日10:00-12:00充值的信息</span><br><span class="line">      <span class="keyword">select</span> * <span class="keyword">from</span> t7</span><br><span class="line">      <span class="keyword">where</span></span><br><span class="line">      <span class="built_in">date</span>(shijian)=<span class="string">"2018-07-31"</span> <span class="keyword">and</span> </span><br><span class="line">      <span class="built_in">time</span>(shijian)&gt;=<span class="string">"10:00:00"</span> <span class="keyword">and</span> </span><br><span class="line">      <span class="built_in">time</span>(shijian)&lt;=<span class="string">"12:00:00"</span>;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h5 id="日期时间运算"><a href="#日期时间运算" class="headerlink" title="日期时间运算"></a>日期时间运算</h5><ol>
<li>语法格式</li>
</ol>
<figure class="highlight vbscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * from 表名</span><br><span class="line">where 字段名 运算符 (时间-interval 时间间隔单位);</span><br><span class="line">    </span><br><span class="line">时间间隔单位：</span><br><span class="line"><span class="number">1</span> <span class="built_in">day</span> | <span class="number">2</span> <span class="built_in">hour</span> | <span class="number">1</span> <span class="built_in">minute</span> | <span class="number">2</span> <span class="built_in">year</span> | <span class="number">3</span> <span class="built_in">month</span></span><br></pre></td></tr></table></figure>
<p>示例<br><figure class="highlight n1ql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">1、查询1天以内的记录</span><br><span class="line">  <span class="keyword">select</span> * <span class="keyword">from</span> t7 </span><br><span class="line">  <span class="keyword">where</span> shijian &gt; (now()-interval <span class="number">1</span> day);</span><br><span class="line">        age     &gt;   20</span><br><span class="line">2、查询1年以前的记录</span><br><span class="line">  <span class="keyword">select</span> * <span class="keyword">from</span> t7</span><br><span class="line">  <span class="keyword">where</span> shijian &lt; (now()-interval <span class="number">1</span> year);</span><br><span class="line">3、查询1天以前,3天以内的记录</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> t7</span><br><span class="line"><span class="keyword">where</span></span><br><span class="line">shijian &lt; (now()-interval <span class="number">1</span> day) <span class="keyword">and</span></span><br><span class="line">shijian &gt; (now()-interval <span class="number">3</span> day);</span><br></pre></td></tr></table></figure></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/数据库/" rel="tag"># 数据库</a>
          
            <a href="/tags/MySQL/" rel="tag"># MySQL</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/02/mysql/教程02/" rel="next" title="Mysql-教程02">
                <i class="fa fa-chevron-left"></i> Mysql-教程02
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/14/python/网络编程01/" rel="prev" title="网络编程基础篇">
                网络编程基础篇 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.JPG" alt="任成">
            
              <p class="site-author-name" itemprop="name">任成</p>
              <div class="site-description motion-element" itemprop="description"></div>
          </div>

          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0NvZGVyQ3VydmU=" title="GitHub &rarr; https://github.com/CoderCurve"><i class="fa fa-fw fa-github"></i>GitHub</span>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="bWFpbHRvOnJlbmNoZW5nMTFAaWNsb3VkLmNvbQ==" title="E-Mail &rarr; mailto:rencheng11@icloud.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#什么是数据库"><span class="nav-number">1.1.</span> <span class="nav-text">什么是数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#都有哪些公司在用数据库"><span class="nav-number">1.2.</span> <span class="nav-text">都有哪些公司在用数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#提供数据库服务的软件"><span class="nav-number">1.3.</span> <span class="nav-text">提供数据库服务的软件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL的特点"><span class="nav-number">1.4.</span> <span class="nav-text">MySQL的特点</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#关系型数据库"><span class="nav-number">1.4.1.</span> <span class="nav-text">关系型数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#跨平台"><span class="nav-number">1.4.1.1.</span> <span class="nav-text">跨平台</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#支持多种编程语言"><span class="nav-number">1.4.1.2.</span> <span class="nav-text">支持多种编程语言</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据库软件、数据库、数据仓库的概念"><span class="nav-number">1.4.2.</span> <span class="nav-text">数据库软件、数据库、数据仓库的概念</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MySQL安装"><span class="nav-number">2.</span> <span class="nav-text">MySQL安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Ubuntu安装软件"><span class="nav-number">2.1.</span> <span class="nav-text">Ubuntu安装软件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#字符类型"><span class="nav-number">2.1.1.</span> <span class="nav-text">字符类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#区别："><span class="nav-number">2.1.1.1.</span> <span class="nav-text">区别：</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#枚举类型"><span class="nav-number">2.1.2.</span> <span class="nav-text">枚举类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#日期时间类型"><span class="nav-number">2.1.3.</span> <span class="nav-text">日期时间类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#日期时间函数"><span class="nav-number">2.1.3.1.</span> <span class="nav-text">日期时间函数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#日期时间运算"><span class="nav-number">2.1.3.2.</span> <span class="nav-text">日期时间运算</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">  <span class="exturl" data-url="aHR0cDovL3d3dy5iZWlhbi5taWl0Lmdvdi5jbg==">京ICP备18001214号 </span>&copy; 2014 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">任成</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
    <span title="站点总字数">277k</span>
  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.2"></script>

  <script src="/js/motion.js?v=7.1.2"></script>



  
  


  <script src="/js/affix.js?v=7.1.2"></script>

  <script src="/js/schemes/pisces.js?v=7.1.2"></script>



  
  <script src="/js/scrollspy.js?v=7.1.2"></script>
<script src="/js/post-details.js?v=7.1.2"></script>



  


  <script src="/js/next-boot.js?v=7.1.2"></script>


  

  
  <script src="/js/exturl.js?v=7.1.2"></script>


  

  


  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
  

  
  

  


  

  
<script>
if ($('body').find('pre.mermaid').length) {
  $.ajax({
    type: 'GET',
    url: '//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js',
    dataType: 'script',
    cache: true,
    success: function() {
      mermaid.initialize({
        theme: 'forest',
        logLevel: 3,
        flowchart: { curve: 'linear' },
        gantt: { axisFormat: '%m/%d/%Y' },
        sequence: { actorMargin: 50 }
      });
    }
  });
}
</script>


  

  

  

  

  

  

  

  

  

</body>
</html>
